/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.spaceandtime.mvc;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.apache.log4j.Logger;
import za.co.pas.business.abstracts.AbstractController;
import za.co.pas.business.message.MessageObj;
import za.co.pas.business.message.StatusMessage;
import za.co.pas.business.message.StatusMessageObject;
import za.co.pas.business.abstracts.AbstractDataModel;
import za.co.pas.spaceandtime.controller.BuildingDetailController;
import za.co.pas.spaceandtime.model.BuildingDataModel;
import za.co.pas.spaceandtime.model.ConfigDataModel;
import za.co.pas.spaceandtime.model.ext.BuildingDetailModel;

/**
 *
 * @author alabuschagne
 */
public class SpaceAndTimeMVC implements PropertyChangeListener
{

    public static final int BUILDING_DETAIL_CONTROLLER = 0;
    public static final int BUILDING_DETAIL_MODEL = 0;
    public static final int CONFIG_DATA_MODEL = 1;
    private BuildingDetailController buildingDetailController;
    private BuildingDetailModel buildingDetailModel;
    private ConfigDataModel configDataModel;
    private boolean init = false;
    protected PropertyChangeSupport propertyChangeSupport;
    private static Logger logger = Logger.getLogger(SpaceAndTimeMVC.class);

    public SpaceAndTimeMVC()
    {
        propertyChangeSupport = new PropertyChangeSupport(this);
    }
    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    protected void firePropertyChange(MessageObj msgObj)
    {
        propertyChangeSupport.firePropertyChange(msgObj.getPropertyName(), msgObj.getOldValue(), msgObj.getNewValue());
    }

    public void init()
    {
        firePropertyChange(new StatusMessage(StatusMessageObject.SMO_INFO, "Initialising database connection...",StatusMessageObject.SMO_TYPE_BUSY_START));
        BuildingDataModel bdm = new BuildingDataModel();
        buildingDetailModel = new BuildingDetailModel(bdm);
        buildingDetailController = new BuildingDetailController();
        buildingDetailController.addModel(buildingDetailModel);
        configDataModel = new ConfigDataModel();
        firePropertyChange(new StatusMessage(StatusMessageObject.SMO_INFO, "Initialised database connection",StatusMessageObject.SMO_TYPE_BUSY_END));
        if(buildingDetailModel.isConnectionOk())
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_INFO, MessageObj.ALLOK, StatusMessageObject.SMO_TYPE_DB));
        }
        else
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, MessageObj.ALLOK, StatusMessageObject.SMO_TYPE_DB));
        }
        init = true;
    }

    public AbstractDataModel getModel(int which)
    {
        switch (which)
        {
            case BUILDING_DETAIL_MODEL:
            {
                return buildingDetailModel;
            }
            case CONFIG_DATA_MODEL:
            {
                return configDataModel;
            }
            default:
            {
                return null;
            }
        }
    }

    public AbstractController getController(int which)
    {
        switch (which)
        {
            case BUILDING_DETAIL_CONTROLLER:
            {
                return buildingDetailController;
            }
            default:
            {
                return null;
            }
        }
    }

    public boolean isInit()
    {
        return init;
    }

    public boolean canInit()
    {
        return propertyChangeSupport.getPropertyChangeListeners().length > 0;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt)
    {
        StringBuilder sb = new StringBuilder("[SpaceAndTimeMVC] ");
        sb.append(evt.getPropertyName());
        if(evt.getOldValue() != null)
        {
            sb.append("|");
            sb.append(evt.getOldValue().toString());
        }
        if(evt.getNewValue() != null)
        {
            sb.append("|");
            sb.append(evt.getNewValue().toString());
        }
        System.out.println(sb.toString());
    }
}
